<template>
  <el-dialog :visible.sync="isShow" @close="handleCancel" width="60%" :close-on-click-modal="false" :title="user==0?'实名认证':'修改修改实名认证'">

    <el-form :rules="rules" ref="ruleForm" :model="ruleForm" label-width="150px" v-loading='loading'>
      <div class="relboxmain">
        <div class="relbox">
          <el-form-item label="身份证正面" prop="sfzqm">
            <el-upload :headers="headers" :data="{'types':1,'dataUp': { dirName: '' }}" name="positive" :limit="1" list-type="picture-card" :file-list="sfzqmList" :on-error="sfzqmError" :on-remove="sfzqmRemove" :action="uploadPath" :on-success="sfzqmSuccess" :before-upload="beforeAvatarUpload">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="身份证反面" prop="sfzhm">
            <el-upload :headers="headers" :data="{'types':2,'dataUp': { dirName: '' }}" name="positive" :limit="1" list-type="picture-card" :file-list="sfzhmList" :on-error="sfzhmError" :on-remove="sfzhmRemove" :action="uploadPath" :on-success="sfzhmSuccess" :before-upload="beforeAvatarUpload">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="姓名：" prop="userName">
            <el-input readonly :value="ruleForm.userName" />
          </el-form-item>
          <el-form-item label="身份证号码：" prop="userCardNum">
            <el-input readonly :value="ruleForm.userCardNum" />
          </el-form-item>

        </div>
        <div class="relbox">
          <el-form-item label="营业执照" prop="yyzz">
            <el-upload :headers="headers" :data='{dirName:""}' :limit="1" list-type="picture-card" :file-list="yyzzList" :on-error="yyzzError" :on-remove="yyzzRemove" :action="uploadPath2" :on-success="yyzzSuccess" :before-upload="beforeAvatarUpload">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="企业名称：" prop="companyName">
            <el-input v-model="ruleForm.companyName" />
          </el-form-item>
          <el-form-item label="企业信用代码：" prop="companyCode">
            <el-input v-model="ruleForm.companyCode" />
          </el-form-item>
          <el-form-item label="企业通讯地址：" prop="postalAddress">
            <el-input v-model="ruleForm.postalAddress" />
          </el-form-item>
          <el-form-item label="选择银行：" prop="bankCode">
            <el-select v-model="ruleForm.bankCode" clearable filterable placeholder="请选择">
              <el-option v-for="item in bankListL" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开户行省：" prop="bankProvince">
            <el-select v-model="ruleForm.bankProvince" clearable @change="bindProvince" filterable placeholder="请选择">
              <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开户行市：" prop="bankCity">
            <el-select v-model="ruleForm.bankCity" clearable filterable placeholder="请选择">
              <el-option v-for="item in cityList" :key="item.id" :label="item.name" :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="银行卡号：" prop="bankAccount">
            <el-input v-model="ruleForm.bankAccount" />
          </el-form-item>
          <el-form-item label="开户支行：" prop="bankSubname">
            <el-input v-model="ruleForm.bankSubname" />
          </el-form-item>
          <el-form-item label="提现密码：" prop="bankPass">
            <el-input v-model="ruleForm.bankPass" />
          </el-form-item>

        </div>

      </div>

      <el-form-item>
        <el-button :loading='loading' type="primary" @click="handlePublish">保存</el-button>
      </el-form-item>
    </el-form>

  </el-dialog>
</template>

<script>
import {
  uploadPath,
  fileCreate,
  uploadPath2,
  realproveCreate,
  realpositiveUpdate,
  province,
  city
} from '@/api/profile'
import { getToken } from '@/utils/auth'
export default {
  name: 'realName',
  // props: {
  //   isShow:false
  // },
  data() {
    return {
      loading: false,
      isShow: false,
      uploadPath: uploadPath,
      uploadPath2: uploadPath2,
      sfzqmList: [],
      sfzhmList: [],
      yyzzList: [],
      provinceList: [], // 省
      cityList: [], // 市
      cityListOld: [],
      bankListL: [
        { id: 1, name: '工商银行' },
        { id: 3, name: '农业银行' },
        { id: 2, name: '建设银行' },
        { id: 5, name: '中国银行' },
        { id: 7, name: '招商银行' },
        { id: 33, name: '北京银行' },
        { id: 6, name: '交通银行' },
        { id: 13, name: '兴业银行' },
        { id: 65, name: '南京银行' },
        { id: 14, name: '民生银行' },
        { id: 8, name: '光大银行' },
        { id: 18, name: '平安银行' },
        { id: 67, name: '渤海银行' },
        { id: 66, name: '东亚银行' },
        { id: 17, name: '宁波银行' },
        { id: 12, name: '中信银行' },
        { id: 11, name: '广发银行' },
        { id: 16, name: '上海银行' },
        { id: 9, name: '上海浦东发展银行' },
        { id: 4, name: '中国邮政' },
        { id: 10, name: '华夏银行' },
        { id: 68, name: '北京农村商业银行' }
      ],
      ruleForm: {
        birthday: '',
        cardImg: '',
        cardBackImg: '',
        companyCode: '',
        companyName: '',
        companyUrl: '',
        gender: '',
        postalAddress: '',
        userCardNum: '',
        userName: '',
        sfzqm: '',
        sfzhm: '',
        yyzz: '',
        bankCode: '',
        bankProvince: '',
        bankCity: '',
        bankAccount: '',
        bankSubname: '',
        bankPass: ''
      },
      rules: {
        companyName: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        companyCode: [
          { required: true, message: '企业信用代码不能为空', trigger: 'blur' }
        ],
        postalAddress: [
          { required: true, message: '企业地址不能为空', trigger: 'blur' }
        ],
        sfzqm: [
          { required: true, message: '请选择身份证正面', trigger: 'blur' }
        ],
        sfzhm: [
          { required: true, message: '请选择身份证反面', trigger: 'blur' }
        ],
        yyzz: [{ required: true, message: '请选择营业执照', trigger: 'blur' }],
        bankCode: [
          { required: true, message: '请选择银行', trigger: 'change' }
        ],
        bankAccount: [
          { required: true, message: '请填写银行卡号', trigger: 'blur' }
        ],
        bankProvince: [
          { required: true, message: '请选择省', trigger: 'change' }
        ],
        bankCity: [{ required: true, message: '请选择市', trigger: 'change' }],
        bankSubname: [
          { required: true, message: '请填写开户支行', trigger: 'blur' }
        ],
        bankPass: [
          { required: true, message: '请填写提现密码', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    headers() {
      return {
        'X-Hyxxkj-Admin-Token': getToken()
      }
    },
    user() {
      return this.$store.state.user.ulock
    }
  },
  created() {},
  watch: {},
  mounted() {
    province({
      id: 0
    }).then((res) => {
      this.provinceList = res.data.data.list
    })
    city().then((res) => {
      this.cityListOld = res.data.data.list
    })
  },
  methods: {
    bindProvince(e) {
      this.ruleForm.bankCity = ''
      this.cityListOld.map((item) => {
        if (item.code == e) {
          this.cityList = item.children
          this.ruleForm.bankProvince = item.name
        }
      })
    },
    handleCancel() {},
    realnameUpdate() {
      realpositiveUpdate({
        birthday: this.ruleForm.birthday,
        cardImg: this.ruleForm.cardImg,
        cardBackImg: this.ruleForm.cardBackImg,
        companyCode: this.ruleForm.companyCode,
        companyName: this.ruleForm.companyName,
        companyUrl: this.ruleForm.companyUrl,
        gender:
          this.ruleForm.gender == '女'
            ? (this.ruleForm.gender = 2)
            : (this.ruleForm.gender = 1),
        postalAddress: this.ruleForm.postalAddress,
        userCardNum: this.ruleForm.userCardNum,
        userName: this.ruleForm.userName,

        bankCode: this.ruleForm.bankCode, // 银行代码
        bankAccount: this.ruleForm.bankAccount, // 开户账号
        bankProvince: this.ruleForm.bankProvince, // 开户行省
        bankCity: this.ruleForm.bankCity, // 开户行市
        bankSubname: this.ruleForm.bankSubname, // 开户支行
        bankPass: this.ruleForm.bankPass // 提现密码
      })
        .then((res) => {
          this.loading = false
          this.$notify.success({
            title: '成功',
            message: '修改实名认证已提交',
            duration: 3000,
            onClose: () => {
              location.reload()
            }
          })
        })
        .catch((res) => {
          this.loading = false
          this.$notify.error({
            title: '错误',
            message: res.data.errmsg,
            duration: 6000
          })
        })
    },

    realnameCreate() {
      var query = {
        birthday: this.ruleForm.birthday,
        cardImg: this.ruleForm.cardImg,
        cardBackImg: this.ruleForm.cardBackImg,
        companyCode: this.ruleForm.companyCode,
        companyName: this.ruleForm.companyName,
        companyUrl: this.ruleForm.companyUrl,
        gender:
          this.ruleForm.gender == '女'
            ? (this.ruleForm.gender = 2)
            : (this.ruleForm.gender = 1),
        postalAddress: this.ruleForm.postalAddress,
        userCardNum: this.ruleForm.userCardNum,
        userName: this.ruleForm.userName,
        realType: 2, // 1企业认证  2个人认证
        bankCode: this.ruleForm.bankCode, // 银行代码
        bankAccount: this.ruleForm.bankAccount, // 开户账号
        bankProvince: this.ruleForm.bankProvince, // 开户行省
        bankCity: this.ruleForm.bankCity, // 开户行市
        bankSubname: this.ruleForm.bankSubname, // 开户支行
        bankPass: this.ruleForm.bankPass // 提现密码
      }
      realproveCreate(query)
        .then((res) => {
          this.loading = false
          this.$notify.success({
            title: '成功',
            message: '实名认证已提交',
            duration: 3000,
            onClose: () => {
              location.reload()
            }
          })
        })
        .catch((res) => {
          console.log(res.data.errmsg)
          this.loading = false
          this.$notify.error({
            title: '错误',
            message: res.data.errmsg,
            duration: 6000
          })
        })
    },

    handlePublish() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          var ulock = this.$store.state.user.ulock
          if (ulock == 0) {
            this.realnameCreate()
            return
          }
          if (ulock == 2) {
            this.realnameUpdate()
            return
          }
        } else {
          this.$message({
            message: '请检查数据必填项',
            type: 'warning'
          })
          return false
        }
      })
    },
    sfzqmError(err, file) {
      // 上传出错
      this.ruleForm.sfzqm = ''
      this.$notify.error({
        title: '错误',
        message: '身份证正面上传出错',
        duration: 6000
      })
    },
    sfzqmSuccess(res, file) {
      // 上传成功
      if (res.errno == 0) {
        this.ruleForm.birthday = res.data.birthday
        this.ruleForm.userCardNum = res.data.idcardNum
        this.ruleForm.userName = res.data.username
        this.ruleForm.gender = res.data.sex
        var Formqm = new FormData()
        Formqm.append('file', file.raw)
        fileCreate(Formqm)
          .then((res) => {
            if (res.data.errno == 0) {
              this.ruleForm.cardImg = res.data.data.url
              this.ruleForm.sfzqm = file.raw
              this.$notify.success({
                title: '成功',
                message: '身份证正面识别成功'
              })
            } else {
              this.$notify.error({
                title: '错误',
                message: res.data.errmsg,
                duration: 6000
              })
            }
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        this.$notify.error({
          title: '错误',
          message: res.errmsg,
          duration: 6000
        })
      }
    },
    sfzqmRemove() {
      // 移除图片
      this.ruleForm.sfzqm = ''
    },

    sfzhmError(err, file) {
      // 上传出错
      this.ruleForm.sfzhm = ''
      this.$notify.error({
        title: '错误',
        message: '身份证正面上传出错',
        duration: 6000
      })
    },
    sfzhmSuccess(res, file) {
      // 上传成功
      if (res.errno == 0) {
        var Formqm = new FormData()
        Formqm.append('file', file.raw)
        fileCreate(Formqm)
          .then((res) => {
            if (res.data.errno == 0) {
              this.ruleForm.cardBackImg = res.data.data.url
              this.ruleForm.sfzhm = file.raw
              this.$notify.success({
                title: '成功',
                message: '身份证反面识别成功'
              })
            } else {
              this.$notify.error({
                title: '错误',
                message: res.data.errmsg,
                duration: 6000
              })
            }
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        this.$notify.error({
          title: '错误',
          message: res.errmsg,
          duration: 6000
        })
      }
    },
    sfzhmRemove() {
      // 移除图片
      this.ruleForm.sfzhm = ''
    },

    yyzzError(err, file) {
      // 上传出错
      this.ruleForm.yyzz = ''
      this.$notify.error({
        title: '错误',
        message: '身份证正面上传出错',
        duration: 6000
      })
    },
    yyzzSuccess(res, file) {
      // 上传成功
      if (res.errno == 0) {
        this.ruleForm.yyzz = file.raw
        this.ruleForm.companyUrl = res.data.url
        this.$notify.success({
          title: '成功',
          message: '营业执照上传成功'
        })
      } else {
        this.$notify.error({
          title: '错误',
          message: res.errmsg,
          duration: 6000
        })
      }
    },
    yyzzRemove() {
      // 移除图片
      this.ruleForm.yyzz = ''
    },

    beforeAvatarUpload(file) {
      // 图片改变
      var isJPG = false
      if (
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/gif'
      ) {
        isJPG = true
      }
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG/GIF 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }

      return isJPG && isLt2M
    }
  }
}
</script>

<style lang="scss" scoped>
.relboxmain {
  display: flex;
  justify-content: space-between;
}
.relbox {
  flex: 1;
}
</style>
